<?php
namespace App\Actions;

use App\Models\AdminUser;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class DzzHookAction extends BaseAction
{
    public $model;
    public $admin_user;

    public function __construct(Model $model = null, AdminUser $adminUser = null)
    {
        $this->admin_user = $adminUser;
    }

    public function import($all_datas, $table)
    {
        foreach($all_datas as $_data) {
            # 判断数据是否重复
            $_data['dwmc'] = $this->admin_user->dwmc;
            $_data['dwdm'] = $this->admin_user->dwdm;
            $_data['updated_by_gid'] = $this->admin_user->gid;
            $_data['updated_at'] = date("Y-m-d H:i:s");
            if (!isset($_data['dzzmc'])) {
                continue;
            }
            if ($table == 'dzz_yjdzz_infos') {
                if (isset($_data['ssdzzmc']) && $_data['ssdzzmc'] && $model = DB::table($table)->where('ssdzzmc', $_data['ssdzzmc'])->where('dwdm', $this->admin_user->dwdm)->first()) {
                    DB::table($table)->where('ssdzzmc', $_data['ssdzzmc'])->where('dwdm', $this->admin_user->dwdm)->update($_data);
                    $id = $model->id;
                } else {
                    $_data['created_by_gid'] = $this->admin_user->gid;
                    $_data['created_at'] = date("Y-m-d H:i:s");
                    $id = DB::table($table)->insertGetId($_data);
                }
            } else {
                if ($_data['dzzmc'] && $model = DB::table($table)->where('dzzmc', $_data['dzzmc'])->where('dwdm', $this->admin_user->dwdm)->first()) {
                    DB::table($table)->where('dzzmc', $_data['dzzmc'])->where('dwdm', $this->admin_user->dwdm)->update($_data);
                    $id = $model->id;
                } else {
                    $_data['created_by_gid'] = $this->admin_user->gid;
                    $_data['created_at'] = date("Y-m-d H:i:s");
                    $id = DB::table($table)->insertGetId($_data);
                }
            }
        }
    }

    /**
     * 处理修改的数据
     */
    public function save($inputs, Model $model = null)
    {
        if (isset($inputs['_inline_edit_']) && $inputs['_inline_edit_']) {
            # 行内编辑
            return $this->dealInlineData($inputs, $model);
        }

        $inputs = collect($inputs)->except(['_token', '_method', '_previous_'])->all();

        $table = $model->getTable();
        $modelClass = get_class($model);
        $is_edit = $model->id ? true : false;

        if ($table == 'dzz_yjdzz_infos') {
            if (!$is_edit) {
                # 新增
                if ($modelClass::query()->where('ssdzzmc', $inputs['ssdzzmc'])->where('dwdm', $this->admin_user->dwdm)->exists()) {
                    $this->error($table == 'dzz_infos' ? '党支部名称已经存在' : '院级党组织所属党总支名称已经存在');
                }
            } else {
                # 修改
                if ($modelClass::query()->where('ssdzzmc', $inputs['ssdzzmc'])->where('dwdm', $model->dwdm)->where('id', '<>', $model->id)->exists()) {
                    $this->error($table == 'dzz_infos' ? '党支部名称已经存在' : '院级党组织所属党总支名称已经存在');
                }
            }
        } else {
            if (!$is_edit) {
                # 新增
                if ($modelClass::query()->where('dzzmc', $inputs['dzzmc'])->where('dwdm', $this->admin_user->dwdm)->exists()) {
                    $this->error($table == 'dzz_infos' ? '党支部名称已经存在' : '院级党组织名称已经存在');
                }
            } else {
                # 修改
                if ($modelClass::query()->where('dzzmc', $inputs['dzzmc'])->where('dwdm', $model->dwdm)->where('id', '<>', $model->id)->exists()) {
                    $this->error($table == 'dzz_infos' ? '党支部名称已经存在' : '院级党组织名称已经存在');
                }
            }
        }
        
        $filter_data = collect($inputs)->only($modelClass::$edit_fields)->filter()->all();
        $errors = $modelClass::validators($filter_data);
        if ($errors) {
            $inputs['is_error'] = 1;
            $inputs['errors'] = json_encode($errors);
            $this->error(implode("<br/>", $errors));
        }
        $inputs['is_error'] = 0;
        $inputs['errors'] = json_encode([]);
        $inputs['updated_by_gid'] = $this->admin_user->gid;
        $inputs['updated_at'] = date("Y-m-d H:i:s");
        if (!$is_edit) {
            $inputs['dwdm'] = $this->admin_user->dwdm;
            $inputs['dwmc'] = $this->admin_user->dwmc;
            $inputs['created_by_gid'] = $this->admin_user->gid;
            $modelClass::create($inputs);
        } else {
            $model->update($inputs);
        }
    }

    /**
     * 党员信息行内编辑
     */
    public function dealInlineData($inputs, Model $model)
    {
        $modelClass = get_class($model);
        $filter_data = collect($inputs)->only($modelClass::$edit_fields)->all();
        $field = '';
        foreach(collect($filter_data)->keys() as $key) {
            $field = $key ? $key : '';
        }
        if (!$field) {
            $this->error('此处无法修改，请使用编辑按钮进行数据修改');
        }
        $errors = $modelClass::validatorsFoInline($field, collect($filter_data)->filter()->all(), $model->toArray());
        if ($errors) {
            $this->error(implode("<br/>", $errors));
        }

        $update_data = $filter_data;
        $update_data['is_error'] = 0;
        $update_data['errors'] = json_encode([]);
        $inputs['updated_by_gid'] = $this->admin_user->gid;
        $inputs['updated_at'] = date("Y-m-d H:i:s");
        $model->update($update_data);
    }
}